package com.xoa.service.equipment.impl;

import com.xoa.dao.equipment.LimsEquipEventsCapyMapper;
import com.xoa.model.equipment.LimsEquipEventsCapy;
import com.xoa.service.equipment.LimsEquipEventsCapyService;
import com.xoa.util.LimsJson;
 import com.xoa.util.page.PageParams;
import net.sf.json.JSONArray;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;



/**
 * @作者 杨岩林
 * @创建日期 12:112019/12/23
 * @类介绍
 */
@Service
public class LimsEquipEventsCapyServiceImpl implements LimsEquipEventsCapyService {

    @Autowired
    private LimsEquipEventsCapyMapper limsEquipEventsCapyMapper;

    @Override
    public LimsJson deleteByIds(String ids) {
        LimsJson limsJson=new LimsJson(1,"err");
        String[] idArray=ids.split(",");
        int result=limsEquipEventsCapyMapper.deleteByIds(idArray);
        if(result>0){
            limsJson.setMsg("删除成功");
            limsJson.setCode(0);
        }
        return limsJson;
    }

    @Override
    public LimsJson showByEventLogId(Integer eventLogId, Integer page, Integer limit) {
        LimsJson limsJson=new LimsJson(1,"err");
        Map map=new HashMap();
        PageParams pageParams=new PageParams();
        pageParams.setPage(page);
        pageParams.setPageSize(limit);
        map.put("page",pageParams);
        map.put("eventLogId",eventLogId);
        List<LimsEquipEventsCapy> limsEquipEventsCapyList=limsEquipEventsCapyMapper.showByEventLogId(map);
        if(limsEquipEventsCapyList!=null&& limsEquipEventsCapyList.size()>0){
            limsJson.setCode(0);
            limsJson.setMsg("查询成功");
            limsJson.setData(limsEquipEventsCapyList);
            limsJson.setCount(pageParams.getTotal());
        }else{
            limsJson.setCode(0);
            limsJson.setMsg("无数据");
        }
        return limsJson;
    }

    @Override
    public LimsJson addOrUpdate(String json) {
        LimsJson limsJson=new LimsJson(1,"err");
        int a=0,b=0,c=0,d=0;
        JSONArray jsonarray = JSONArray.fromObject(json);
        List<LimsEquipEventsCapy> limsEquipEventsCapyList = (List<LimsEquipEventsCapy>) JSONArray.toCollection(jsonarray, LimsEquipEventsCapy.class);
        for (LimsEquipEventsCapy limsEquipEventsCapy : limsEquipEventsCapyList) {
            if(limsEquipEventsCapy.getEventCapyId()!=null){
                a++;
                int result=limsEquipEventsCapyMapper.updateByPrimaryKeySelective(limsEquipEventsCapy);
                if(result>0){
                    b++;
                }
            }else{
                c++;
                int result=limsEquipEventsCapyMapper.insertSelective(limsEquipEventsCapy);
                if(result>0){
                    d++;
                }
            }
        }
        if(a==b&& c==d){
            limsJson.setMsg("保存成功");
            limsJson.setCode(0);
        }
        return limsJson;
    }
}
